package cn.signalpha.modules.shop.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.modules.shop.dto.PlaceApplyDTO;
import cn.signalpha.modules.shop.entity.PlaceApply;
import cn.signalpha.modules.shop.service.PlaceApplyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/wap/place/apply")
@Api(value = "分销员申请", tags = "分销员申请")
public class ApiPlaceApplyController extends BaseController {

	private final PlaceApplyService placeApplyService;

    @GetMapping("/page")
    @ApiOperation(value = "申请记录", notes = "")
    public R<IPage<PlaceApply>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("userId", getUser().getUserId());
        QueryWrapper<PlaceApply> wrapper = placeApplyService.selectPlaceApplyWrapper(params);
        return R.data(placeApplyService.page(Condition.getPage(query), wrapper));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<PlaceApply> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(placeApplyService.getById(id));
	}

	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入placeApply")
	public R save(@Valid @RequestBody PlaceApplyDTO placeApplyDTO) {
		placeApplyDTO.setStatus(0);
		placeApplyDTO.setUserId(getUser().getUserId());
		placeApplyDTO.setCreateTime(new Date());
		return R.status(placeApplyService.save(BeanUtil.copy(placeApplyDTO, PlaceApply.class)));
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入placeApply")
	public R update(@Valid @RequestBody PlaceApplyDTO placeApplyDTO) {
		PlaceApply placeApply = placeApplyService.getById(placeApplyDTO.getId());
		Assert.isNull(placeApply, MsgConstant.OBJECT_NULL);
		Assert.isFalse(placeApply.getStatus() == 1, "已申请通过不可修改");
		placeApply.setStatus(0);
		placeApply.setCreateTime(new Date());
		return R.status(placeApplyService.updateById(BeanUtil.copy(placeApplyDTO, PlaceApply.class)));
	}

}
